package com.ruoyi.server.MQTT.config;

import io.moquette.broker.Server;
import io.moquette.broker.config.MemoryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Properties;

@Service
public class MqttBrokerConfig {
    
    @Bean
    public Server mqttBroker() throws IOException {
        Server moquetteServer = new Server();
        Properties configProps = new Properties();
        // 基础配置
        configProps.setProperty("port", "9009");
        configProps.setProperty("host", "0.0.0.0");
        configProps.setProperty("websocket_port", "8087"); // WebSocket支持[3](@ref)
        // 持久化配置
        configProps.setProperty("persistent_store", "true");
        configProps.setProperty("storage_path", "./moquette_store");
        
        moquetteServer.startServer(new MemoryConfig(configProps));
        Runtime.getRuntime().addShutdownHook(new Thread(moquetteServer::stopServer));
        return moquetteServer;
    }
}
